from sqlalchemy import ForeignKey, Column, Integer, String
from sqlalchemy.orm import mapped_column, relationship
from sqlalchemy.orm import Mapped
from typing import List

from app.models.base import Base


class User(Base):
    __tablename__ = "user"
    name: Mapped[str] = mapped_column(String(30))
    transform: Mapped[List["Transform"]] = relationship(
        back_populates="user", cascade="all, delete-orphan"
    )

    def sum_gen(self):
        return sum([t.gen for t in self.transform])

    def sum_gen_get(self):
        return sum([t.gen for t in self.transform if t.gen > 0])

    def sum_gen_use(self):
        return -sum([t.gen for t in self.transform if t.gen < 0])


# 定义 User 模型
class Transform(Base):
    __tablename__ = "transform"
    user_id: Mapped[int] = mapped_column(ForeignKey("user.id"))
    gen = Column(Integer)
    user: Mapped["User"] = relationship(back_populates="transform")
